<template>
  <el-dialog title="添加操作步骤" v-model="props.dialogVisible" width="50%" :before-close="handleClose"
             class="operation-steps-dialog" align-center append-to-body>
    <el-form :model="form" label-width="120px">
      <el-input type="hidden" v-model="props.code"/>
      <el-row>
        <el-col :span="12">
          <el-form-item label="顺序编号">
            <el-input-number
                v-model="form.orderNumber"
                :min="1"
                class="w-32"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材料URL">
            <el-input v-model="form.materialUrl" placeholder="请输入材料URL"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="辅助设备">
            <el-input v-model="form.auxiliaryEquipment" placeholder="请输入辅助设备"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="辅助材料">
            <el-input v-model="form.auxiliaryMaterials" placeholder="请输入辅助材料"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="步骤说明">
        <el-input v-model="form.stepsDescription" type="textarea" rows="3" placeholder="请输入内容"/>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" rows="3" placeholder="请输入内容"/>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref, watch} from 'vue'
import {ElMessage} from "element-plus";
import {addDetails} from "@/apis/zyj/ProcessSettings.js";

const props = defineProps({
  dialogVisible: Boolean,
  code:Object
})

const emit = defineEmits(['details_close'])

// Form data
const form = ref({
  orderNumber: 1,
  materialUrl: '',
  processCode:'',
  auxiliaryEquipment: '',
  auxiliaryMaterials: '',
  stepsDescription: '',
  remark: ''
})
const resetForm =ref({...form.value})

const handleClose = () => {
  Object.assign(form.value, resetForm.value)
  emit('details_close')
}

const handleConfirm = () =>{
  form.value.processCode=props.code
  addDetails(form.value).then(req => {
    if (req === '成功') {
      ElMessage.success('添加成功')
      handleClose()
    } else {
      ElMessage.error('添加失败')
    }
  })
}
</script>

<style scoped>
.operation-steps-dialog {
  :deep(.el-dialog__body) {
    padding: 20px;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>